package com.wman.sheep.common.utils;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class DensityUtil {
    // dp转px
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    // px转dp
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 将px值转换为sp
     *
     * @param context
     * @param pxValue
     * @return
     */
    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    /**
     * 将sp值转换为px
     *
     * @param context
     * @param spValue
     * @return
     */
    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    /**
     * 获得屏幕物理像素width
     * @param context
     * @return
     */
    public static int getScreenWidth(Activity context){
        DisplayMetrics dm = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }
    /**
     * 获得屏幕物理像素height
     * @param context
     * @return
     */
    public static int getScreenHeight(Activity context){
        DisplayMetrics dm = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.heightPixels;
    }

    /**
     * 获得屏幕密度DPI
     * @param context
     * @return
     */
    public static int getDensityDpi(Activity context){
        DisplayMetrics dm = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.densityDpi;
    }

    /**
     * textview大小设置
     * @param Context
     * @param size
     * @param tv
     */
    public  static void setTextSize(Activity Context,double size,TextView... tv){
        for (TextView textView : tv) {
            textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, (int)(getScreenHeight(Context)*size));
        }
    }


    public static void setViewPadding(Activity Context,double left,double top,double right,double bottom,View... views){
        for (View v:views) {
            int width=getScreenWidth(Context);
            int height=getScreenHeight(Context);
            v.setPadding((int)(width*left),(int)(height*top),(int)(width*right),(int)(height*bottom));
        }
    }


    /**
     * Button上text大小设置
     * @param Context
     * @param size
     * @param btn
     */
    public  static void setButtonSize(Activity Context,double size,Button... btn){
        for (Button button : btn) {
            button.setTextSize(TypedValue.COMPLEX_UNIT_PX, (int)(getScreenHeight(Context)*size));
        }
    }
}
